<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PaymentBankDepositForm
 *
 * @author Argenis Fontalvo [argenisfd@gmail.com]
 */
class PaymentBankDepositForm extends BaseInvoicePaymentForm {
    
    private $infoFields=array();
    
    public function configure(){
        if($this->getObject()->isNew()){
           $this->getObject()->setAmount($this->getObject()->getInvoice()->getTotalPrice()); 
           $this->getObject()->setPaymentStatus(PaymentStatusTable::getInstance()->findOneByCode(PaymentStatusTable::STATUS_PENDING));
        }
        $fields=Doctrine_Query::create()
                ->from("PaymentInfoField ".PaymentInfoFieldTable::SHORT_ALIAS." indexby code"  )
                ->whereIn( "code", array(PaymentInfoFieldTable::CODE_BANK,
                                       PaymentInfoFieldTable::CODE_VERIFICATION_CODE,
                                       PaymentInfoFieldTable::CODE_TRANSACTION_DATE,
                                       PaymentInfoFieldTable::CODE_USER_OBSERVATION
                                ) )->execute();
        $this->infoFields=$fields;
        $Banks=array("Banesco"=>"Banesco",  "Mercantil"=>"Mercantil" ); 
        $this->widgetSchema[PaymentInfoFieldTable::CODE_BANK]=new sfWidgetFormChoice(array("choices"=>$Banks));
        $this->widgetSchema->setLabel(PaymentInfoFieldTable::CODE_BANK, $fields[PaymentInfoFieldTable::CODE_BANK]->getName());
        
        $this->widgetSchema[PaymentInfoFieldTable::CODE_VERIFICATION_CODE]=new sfWidgetFormInputText();
        $this->widgetSchema->setLabel(PaymentInfoFieldTable::CODE_VERIFICATION_CODE, $fields[PaymentInfoFieldTable::CODE_VERIFICATION_CODE]->getName());
        
        $this->widgetSchema[PaymentInfoFieldTable::CODE_TRANSACTION_DATE]=new sfWidgetFormDate();
        $this->widgetSchema->setLabel(PaymentInfoFieldTable::CODE_TRANSACTION_DATE, $fields[PaymentInfoFieldTable::CODE_TRANSACTION_DATE]->getName());
        $this->widgetSchema[PaymentInfoFieldTable::CODE_USER_OBSERVATION]=new sfWidgetFormTextarea();
        $this->widgetSchema->setLabel(PaymentInfoFieldTable::CODE_USER_OBSERVATION, $fields[PaymentInfoFieldTable::CODE_USER_OBSERVATION]->getName());
        
        $this->useFields(array("payment_type_id",
                                PaymentInfoFieldTable::CODE_BANK,
                                PaymentInfoFieldTable::CODE_VERIFICATION_CODE,
                                PaymentInfoFieldTable::CODE_TRANSACTION_DATE,
                                PaymentInfoFieldTable::CODE_USER_OBSERVATION
            ));
        
        $this->setValidator(PaymentInfoFieldTable::CODE_BANK,new sfValidatorChoice(array("choices"=>  array_keys($Banks))));
        $this->setValidator(PaymentInfoFieldTable::CODE_VERIFICATION_CODE,
                            new sfValidatorAnd(array(
                            new sfValidatorString(array(
                            'required' => true,
                            'trim' => true,
                            'min_length' => 4,
                            'max_length' => 50
                            )),
                            // Usernames should be safe to output without escaping and generally username-like.
                            /*new sfValidatorRegex(array(
                            'pattern' => '/^\w+$/'
                            ), array('invalid' => 'Usernames must contain only letters, numbers and underscores.')),*/
                            /*new sfValidatorDoctrineUnique(array(
                            'model' => 'sfGuardUser',
                            'column' => 'username'
                            ), array('invalid' => 'There is already a user by that name. Choose another.'))*/
                        )));
        $this->setValidator(PaymentInfoFieldTable::CODE_TRANSACTION_DATE, new sfValidatorDate());
        $this->setValidator(PaymentInfoFieldTable::CODE_USER_OBSERVATION, new sfValidatorString(array(
                                                                            'required' => false,
                                                                            'trim' => false,
                                                                            'min_length' => 0,
                                                                            'max_length' => 255
                                                                            )));
    }
    
    public function doSave($con = null) {
        $paymentInfos=new Doctrine_Collection("InvoicePaimentInfo");
        $paymentInfos->setKeyColumn("field");
        
        
        foreach($this->getObject()->getPaymentInfos() as $key=>$InvoicePaimentInfo){
            $paymentInfos->add($InvoicePaimentInfo);
        }
        
        foreach($this->infoFields as $code=>$field){
            $info=$paymentInfos->get($field->getCode());
            $info->setPaymentInfoField($field);
            $info->setValue($this->getValue($field->getCode()));
            $info->setInvoicePayment($this->getObject());
            $paymentInfos->add($info);
        }
        $this->getObject()->setPaymentInfos($paymentInfos);
        parent::doSave($con);
    }
}

?>
